package com.five.controller.backstage;

import com.five.dao.backstage.IMenuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: 越川豪
 * @Date: 2020/12/1 19:57
 */
@Controller
@RequestMapping("back")
public class BackIndexController {

    @Autowired
    private IMenuDao menuDao;

    //查询菜单
    @RequestMapping("index")
    public String getAllMenu(HttpServletRequest req, HttpServletResponse resp, HttpSession session) throws ServletException, IOException {
        /**查询出所有的菜单  并且拼接成 固定的格式
         *childrens代表的是当前菜单下面的二级菜单
         *【{ mid    murl   childrens:[{}] }，{}】
         *查询出所有的一级菜单
         *当前的登录人的所有的信息
         */
        //当前的登录人的角色
        String rid = session.getAttribute("roleid")+"";
        List<Map> maplist = menuDao.getAllparMenuByRid(rid);
        if (maplist.size() > 0) {
            //循环的添加   childrens
            for (Map map : maplist) {
                //父菜单的id   根据菜单的id   查找出 这个菜单下面的子菜单
                Object mid = map.get("mid");//一级菜单的id
                //根据mid查询当前菜单的子菜单
                List<Map> childList = menuDao.getMenusByPidAndRid(mid, rid);
                map.put("childrens", childList);
            }
        }
        session.setAttribute("menuList", maplist);
//        req.getRequestDispatcher("/view/backstage/backstage.jsp").forward(req,resp);
//        resp.sendRedirect("/view/backstage/backstage.jsp");
        return "view/backstage/backstage";
    }
}
